<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">负责人</label>
        <el-select
          v-model="query.userId"
          clearable
          style="width: 185px"
          placeholder="负责人"
          class="filter-item"
          :remote-method="getName"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in nameList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getLabs"
              placeholder="请选择实验室"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="负责人" prop="administrator">
            <el-select
              v-model="form.administrator"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getAdmins"
              placeholder="请选择负责人"
            >
              <el-option
                v-for="item in admins"
                :key="item.administrator"
                :label="item.adminName"
                :value="item.administrator"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="温度阈值" prop="temperatureThresholdValue">
            <el-input
              v-model="form.temperatureThresholdValue"
              style="width: 370px"
              type="number"
            />
          </el-form-item>
          <el-form-item label="湿度阈值" prop="humidityThresholdValue">
            <el-input
              v-model="form.humidityThresholdValue"
              style="width: 370px"
              type="number"
            />
          </el-form-item>
          <el-form-item label="压差阈值" prop="pressureThresholdValue">
            <el-input
              v-model="form.pressureThresholdValue"
              style="width: 370px"
              type="number"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="labName" label="实验室" />
        <el-table-column prop="adminName" label="负责人" />
        <el-table-column prop="temperature" label="温度">
          <template slot-scope="scope">
            <span v-if="scope.row.temperature">{{ scope.row.temperature }}°C</span>
            <span v-if="scope.row.temperature == null">- -</span>
            <!-- {{ scope.row.temperature }}°C -->
          </template>
        </el-table-column>
        <el-table-column prop="temperatureThresholdValue" label="温度阈值">
          <template slot-scope="scope">
            <span v-if="scope.row.temperatureThresholdValue">{{ scope.row.temperatureThresholdValue }}°C</span>
            <span v-if="scope.row.temperatureThresholdValue == null">- -</span>
            <!-- {{ scope.row.temperatureThresholdValue }}°C -->
          </template>
        </el-table-column>
        <el-table-column prop="humidity" label="湿度">
          <template slot-scope="scope">
            <span v-if="scope.row.humidity">{{ scope.row.humidity }}%</span>
            <span v-if="scope.row.humidity == null">- -</span>
            <!-- {{ scope.row.humidity }}% -->
          </template>
        </el-table-column>
        <el-table-column prop="humidityThresholdValue" label="湿度阈值">
          <template slot-scope="scope">
            <span v-if="scope.row.humidityThresholdValue">{{ scope.row.humidityThresholdValue }}%</span>
            <span v-if="scope.row.humidityThresholdValue == null">- -</span>
            <!-- {{ scope.row.humidityThresholdValue }}% -->
          </template>
        </el-table-column>
        <el-table-column prop="pressure" label="压差">
          <template slot-scope="scope">
            <span v-if="scope.row.pressure">{{ scope.row.pressure }}Pa</span>
            <span v-if="scope.row.pressure == null">- -</span>
            <!-- {{ scope.row.pressure }}Pa -->
          </template>
        </el-table-column>
        <el-table-column prop="pressureThresholdValue" label="压差阈值">
          <template slot-scope="scope">
            <span v-if="scope.row.pressureThresholdValue">{{ scope.row.pressureThresholdValue }}Pa</span>
            <span v-if="scope.row.pressureThresholdValue == null">- -</span>
            <!-- {{ scope.row.pressureThresholdValue }}Pa -->
          </template>
        </el-table-column>
        <el-table-column prop="errorMessage" label="报警信息" />
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'environmentMonitor:edit',
              'environmentMonitor:del',
            ])
          "
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudEnvironmentMonitor from "@/api/environmentMonitor";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get, findAdmin } from "@/api/lab";
import { allNamelist } from "@/api/user";

const defaultForm = {
  id: null,
  name: null,
  labId: null,
  administrator: null,
  temperature: null,
  temperatureThresholdValue: null,
  humidity: null,
  humidityThresholdValue: null,
  pressure: null,
  pressureThresholdValue: null,
  errorMessage: null,
};
export default {
  name: "EnvironmentMonitor",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "环境监控仪",
      url: "api/environmentMonitor",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudEnvironmentMonitor },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "environmentMonitor:add"],
        edit: ["admin", "environmentMonitor:edit"],
        del: ["admin", "environmentMonitor:del"],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        labId: [
          { required: true, message: "实验室不能为空", trigger: "change" },
        ],
        administrator: [
          { required: true, message: "负责人不能为空", trigger: "change" },
        ],
        temperatureThresholdValue: [
          { required: true, message: "温度阈值不能为空", trigger: "blur" },
        ],
        humidityThresholdValue: [
          { required: true, message: "湿度阈值不能为空", trigger: "blur" },
        ],
        pressureThresholdValue: [
          { required: true, message: "压差阈值不能为空", trigger: "blur" },
        ],
      },
      queryTypeOptions: [
        { key: "name", display_name: "名称" },
        { key: "labId", display_name: "实验室" },
        { key: "administrator", display_name: "负责人" },
      ],
      labs: [],
      nameList: [],
      admins: [],
    };
  },
  created() {
    this.getLabs();
    this.getAdmins();
    this.getName();
  },
  methods: {
    getLabs() {
      get()
        .then((response) => {
          this.labs = response;
        })
        .catch((_) => {
          console.log(response);
        });
    },
    getName() {
      allNamelist()
        .then((response) => {
          this.nameList = response.content;
          console.log(this.nameList);
        })
        .catch((_) => {
          // console.log(response);
        });
    },
    getAdmins() {
      findAdmin()
        .then((response) => {
          this.admins = response;
        })
        .catch((_) => {
          console.log(response);
        });
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped>
</style>
